package com.sunrise.xdoc.view.order;

import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.sunrise.xdoc.entity.order.DonHang;
import com.sunrise.xdoc.view.AbstractListDataModel;

@Repository
public class DonHangDataModel extends AbstractListDataModel<DonHang> {
	private static final long serialVersionUID = 606361650211827563L;

	public void setFromDateFilter(Date fromDate) {
		filters.put("fromDate", fromDate);
	}

	public Date getFromDateFilter() {
		return (Date) filters.get("fromDate");
	}

	public void setToDateFilter(Date toDate) {
		filters.put("toDate", toDate);
	}

	public Date getToDateFilter() {
		return (Date) filters.get("toDate");
	}

	public void setMaDonHangFilter(String maDH) {
		filters.put("maDonHang", maDH);
	}

	public String getMaDonHangFilter() {
		return (String) filters.get("maDonHang");
	}

	@Override
	protected void filter(DetachedCriteria criteria, Map<String, Object> filters) {
		Date fromDate = getFromDateFilter();
		Date toDate = getToDateFilter();
		String maDH = getMaDonHangFilter();
		if (fromDate != null) {
			criteria.add(Restrictions.ge("creationDateTime", fromDate));
		}
		if (toDate != null) {
			criteria.add(Restrictions.le("creationDateTime", toDate));
		}
		if (StringUtils.isNotBlank(maDH)) {
			criteria.add(Restrictions.ilike("code", maDH, MatchMode.ANYWHERE));
		}
	}
}
